Public Class RtfTabStripItem
    Inherits TabStripItem

    Private WithEvents RichTextBoxEx1 As New RichTextBoxEx
    Private m_blnInsertMode As Boolean = True
    Private m_intFirstCharOnPage As Integer
    Private m_intSelectionStart As Integer    

    Public Sub New(ByVal title As String)
        MyBase.New(title)
        Me.InitializeComponent()
    End Sub

    Public Overrides ReadOnly Property CanEdit() As Boolean
        Get
            Return Not Me.RichTextBoxEx1.ReadOnly
        End Get
    End Property

    Public Overrides ReadOnly Property CanRedo() As Boolean
        Get
            Return Me.RichTextBoxEx1.CanRedo
        End Get
    End Property

    Public Overrides ReadOnly Property CanUndo() As Boolean
        Get
            Return Me.RichTextBoxEx1.CanUndo
        End Get
    End Property

    Public Sub LoadFile(ByVal file As String)
        Me.RichTextBoxEx1.LoadFile(file)
    End Sub

    Public Overrides Sub OnBeginPrint(ByVal e As System.Drawing.Printing.PrintEventArgs)
        ' Start at the beginning of the text
        m_intFirstCharOnPage = 0
    End Sub

    Public Overrides Sub OnCopy()
        Me.RichTextBoxEx1.Copy()
    End Sub

    Public Overrides Sub OnCut()
        Me.RichTextBoxEx1.Cut()
    End Sub

    Public Overrides Sub OnEndPrint(ByVal e As System.Drawing.Printing.PrintEventArgs)
        ' Clean up cached information
        Me.RichTextBoxEx1.FormatRangeDone()
    End Sub

    Public Overrides Sub OnPaste()
        Me.RichTextBoxEx1.Paste()
    End Sub

    Public Overrides Sub OnPrintPage(ByVal e As System.Drawing.Printing.PrintPageEventArgs)
        ' To print the boundaries of the current page margins
        ' uncomment the next line:
        'e.Graphics.DrawRectangle(System.Drawing.Pens.Blue, e.MarginBounds)

        ' make the RichTextBoxEx calculate and render as much text as will
        ' fit on the page and remember the last character printed for the
        ' beginning of the next page
        m_intFirstCharOnPage = Me.RichTextBoxEx1.FormatRange(False, e, m_intFirstCharOnPage, Me.RichTextBoxEx1.TextLength)

        ' check if there are more pages to print
        If (m_intFirstCharOnPage < Me.RichTextBoxEx1.TextLength) Then
            e.HasMorePages = True
        Else
            e.HasMorePages = False
        End If

        My.Application.DoEvents()
    End Sub

    Public Overrides Sub OnRedo()
        Me.RichTextBoxEx1.Redo()
    End Sub

    Public Overrides Sub OnSelectAll()
        Me.RichTextBoxEx1.SelectAll()
    End Sub

    Public Overrides Sub OnTabSelection()
        With Me.RichTextBoxEx1
            .Select()
            .SelectionStart = m_intSelectionStart
        End With
        Me.RaiseStatusChanged()
    End Sub

    Public Overrides Sub OnUndo()
        Me.RichTextBoxEx1.Undo()
    End Sub

    Public Property [ReadOnly]() As Boolean
        Get
            Return Me.RichTextBoxEx1.ReadOnly
        End Get
        Set(ByVal value As Boolean)
            Me.RichTextBoxEx1.ReadOnly = value
        End Set
    End Property

    Public Property Rtf() As String
        Get
            Return Me.RichTextBoxEx1.Rtf
        End Get
        Set(ByVal value As String)
            Me.RichTextBoxEx1.Rtf = value
        End Set
    End Property

    Public Overrides Property Text() As String
        Get
            Return Me.RichTextBoxEx1.Text.Replace(vbLf, Environment.NewLine)
        End Get
        Set(ByVal value As String)
            Me.RichTextBoxEx1.Text = value
        End Set
    End Property


    Private Sub RichTextBoxEx1_PreviewKeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles RichTextBoxEx1.PreviewKeyDown
        If e.KeyCode = Keys.Insert Then
            m_blnInsertMode = Not m_blnInsertMode
            Me.RaiseStatusChanged()
        End If
    End Sub

    Private Sub RichTextBoxEx1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBoxEx1.SelectionChanged
        m_intSelectionStart = Me.RichTextBoxEx1.SelectionStart
        Me.RaiseStatusChanged()
    End Sub

    Private Sub RichTextBoxEx1_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles RichTextBoxEx1.Validated
        If Me.Parent IsNot Nothing Then CType(Me.Parent, TabStrip).Validate()
    End Sub

    Private Sub InitializeComponent()
        With Me.RichTextBoxEx1
            .Dock = DockStyle.Fill
            .Font = New Font("Courier New", 10)
            .BorderStyle = Windows.Forms.BorderStyle.None
            .RightMargin = 900
            .ScrollBars = RichTextBoxScrollBars.ForcedBoth
            .ShowSelectionMargin = True
            .WordWrap = False
        End With
        Me.Controls.Add(Me.RichTextBoxEx1)
    End Sub

    Private Sub RaiseStatusChanged()
        Dim intLn As Integer
        Dim intCol As Integer

        With Me.RichTextBoxEx1
            intLn = .GetLineFromCharIndex(.SelectionStart) + 1
            intCol = .SelectionStart - .GetFirstCharIndexOfCurrentLine + 1
        End With

        If Me.Parent IsNot Nothing Then CType(Me.Parent, TabStrip).RaiseStatusChanged(intLn, intCol, m_blnInsertMode)
    End Sub

End Class
